package com.kawins.util.colle;

import com.kawins.util.bean.BeanUtil;

import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author rankai
 *         createTime 2017-10-2017/10/31 17:35
 */
public class ListUtil {

    /**
     * 获取集合对象中某个字段值的集合,返回序列化集合
     *
     * @param field
     * @param targetList
     * @param <T>
     * @return
     */
    public static <T> Set<Serializable> listField2SetSer(String field, List<T> targetList) {
        Set<Serializable> set = new HashSet<>(0);
        for (T t : targetList) {
            Object fieldValue = BeanUtil.getFieldValue(t, field);
            if (fieldValue != null) {
                set.add((Serializable) fieldValue);
            }
        }
        return set;
    }

    /**
     * 获取集合对象中某个字段值的集合
     *
     * @param field
     * @param targetList
     * @param <T>
     * @return
     */
    public static <T> Set<Object> listField2Set(String field, List<T> targetList) {
        Set<Object> set = new HashSet<>(0);
        for (T t : targetList) {
            Object fieldValue = BeanUtil.getFieldValue(t, field);
            if (fieldValue != null) {
                set.add(fieldValue.toString());
            }
        }
        return set;
    }


}
